Plotly - Fundamentals - express¶

Documentation link: https://plotly.com/python/plotly-express/

In [1]:
import plotly.express as px
import pandas as pd

Scatter, Line, Area and Bar Charts¶

In [2]:
df = px.data.iris()
df.head()
Out[2]:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1

scatter plots and discrete color¶

In [3]:
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()

trendlines and templates and marginal distribution plots¶

In [4]:
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", 
            marginal_y="violin", marginal_x="box", trendline="ols", template="simple_white")
fig.show()

eror bars¶

In [5]:
df["e"] = df["sepal_width"]/100
fig = px.scatter(df, x="sepal_width", y="sepal_length", 
                color="species", error_x="e", error_y="e")
fig.show()
In [6]:
df = px.data.tips()
df.head()
Out[6]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

bar charts¶

In [7]:
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group")
fig.show()
In [8]:
df = px.data.medals_long()
df.head()
Out[8]:
nation medal count
0 South Korea gold 24
1 China gold 10
2 Canada gold 9
3 South Korea silver 13
4 China silver 15
In [9]:
fig = px.bar(df, x="medal", y="count", color="nation",
            pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"])
fig.show()

facet plots¶

In [10]:
df = px.data.tips()
df.head()
Out[10]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [11]:
fig = px.bar(df, x='sex', y='total_bill', color='smoker', barmode='group', 
            facet_row='time', facet_col='day', 
        category_orders={'day':['Thur','Fri','Sat','Sun'], 'time':['Lunch','Dinner']})
fig.show()
In [12]:
df = px.data.iris()
df.head()
Out[12]:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1

scatterplot matrices¶

In [13]:
fig = px.scatter_matrix(df, dimensions=['sepal_width','sepal_length','petal_width','petal_length'],
                    color='species')
fig.show()

parallel coordinates and parallel categories and continuous color¶

In [14]:
df.head()
Out[14]:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1
In [15]:
# creating labels
iris_labels = {"species_id": "Species",
    "sepal_width": "Sepal Width", "sepal_length": "Sepal Length",
    "petal_width": "Petal Width", "petal_length": "Petal Length", }
# using a color
tealrose_color = px.colors.diverging.Tealrose

fig = px.parallel_coordinates(df, color='species_id', labels=iris_labels,
        color_continuous_scale=tealrose_color, color_continuous_midpoint=2)
fig.show()
In [16]:
df = px.data.tips()
df.head()
Out[16]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [17]:
inferno_color = px.colors.sequential.Inferno

fig = px.parallel_categories(df, color='size', color_continuous_scale=inferno_color)
fig.show()

hover labels¶

In [18]:
df = px.data.gapminder()
df.head()
Out[18]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
In [19]:
df2007 = df.query('year==2007')
df2007.head()
Out[19]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
11 Afghanistan Asia 2007 43.828 31889923 974.580338 AFG 4
23 Albania Europe 2007 76.423 3600523 5937.029526 ALB 8
35 Algeria Africa 2007 72.301 33333216 6223.367465 DZA 12
47 Angola Africa 2007 42.731 12420476 4797.231267 AGO 24
59 Argentina Americas 2007 75.320 40301927 12779.379640 ARG 32
In [20]:
fig = px.scatter(df2007, x='gdpPercap', y='lifeExp', size='pop', color='continent',
            hover_name='country' ,log_x=True, size_max=50)
fig.show()

animations¶

In [21]:
df.head()
Out[21]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
In [22]:
fig = px.scatter(df, x='gdpPercap', y='lifeExp', size='pop', color='continent',
                hover_name='country', facet_col='continent', log_x=True, size_max=40,
                animation_frame='year', animation_group='country', 
                range_x=[100,15000], range_y=[25,90])
fig.show()

line charts¶

In [23]:
df.head()
Out[23]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
In [24]:
fig = px.line(df, x='year', y='lifeExp', color='continent',
            line_group='country', hover_name='country', 
            line_shape='spline', render_mode='svg')
fig.show()

area charts¶

In [25]:
df.head()
Out[25]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
In [26]:
fig = px.area(df, x='year', y='pop', color='continent', line_group='country')
fig.show()

timeline/ Gantt charts¶

In [27]:
df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Resource="Alex"),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])
df
Out[27]:
Task Start Finish Resource
0 Job A 2009-01-01 2009-02-28 Alex
1 Job B 2009-03-05 2009-04-15 Alex
2 Job C 2009-02-20 2009-05-30 Max
In [28]:
fig = px.timeline(df, x_start='Start', x_end='Finish', 
                    y='Resource',color='Task')
fig.show()

funnel charts¶

In [29]:
df_funnel = dict(number=[39, 27.4, 20.6, 11, 2],
    stage=["Website visit", "Downloads", "Potential customers", "Requested price", "Invoice sent"])
print(df_funnel)
display(pd.DataFrame(df_funnel))
{'number': [39, 27.4, 20.6, 11, 2], 'stage': ['Website visit', 'Downloads', 'Potential customers', 'Requested price', 'Invoice sent']}
number stage
0 39.0 Website visit
1 27.4 Downloads
2 20.6 Potential customers
3 11.0 Requested price
4 2.0 Invoice sent
In [30]:
# here we are using dict instead of dataframe
fig = px.funnel(df_funnel, x='number', y='stage')
fig.show()

Part to Whole Charts¶

pie charts¶

In [31]:
df = px.data.gapminder().query('year==2007').query('continent=="Europe"')
df.loc[df['pop']<4e6, 'country'] = 'Other countries'
df.head()
Out[31]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
23 Other countries Europe 2007 76.423 3600523 5937.029526 ALB 8
83 Austria Europe 2007 79.829 8199783 36126.492700 AUT 40
119 Belgium Europe 2007 79.441 10392226 33692.605080 BEL 56
155 Bosnia and Herzegovina Europe 2007 74.852 4552198 7446.298803 BIH 70
191 Bulgaria Europe 2007 73.005 7322858 10680.792820 BGR 100
In [32]:
fig = px.pie(df, values='pop', names='country', 
            title='Population of Europian continent')
fig.show()

sunburst charts¶

In [33]:
df = px.data.gapminder().query('year==2007')
df.head()
Out[33]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
11 Afghanistan Asia 2007 43.828 31889923 974.580338 AFG 4
23 Albania Europe 2007 76.423 3600523 5937.029526 ALB 8
35 Algeria Africa 2007 72.301 33333216 6223.367465 DZA 12
47 Angola Africa 2007 42.731 12420476 4797.231267 AGO 24
59 Argentina Americas 2007 75.320 40301927 12779.379640 ARG 32
In [34]:
fig = px.sunburst(df, path=['continent', 'country'], 
                values='pop', color='lifeExp', hover_data=['iso_alpha'])
fig.show()

treemaps¶

In [35]:
df.head()
Out[35]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
11 Afghanistan Asia 2007 43.828 31889923 974.580338 AFG 4
23 Albania Europe 2007 76.423 3600523 5937.029526 ALB 8
35 Algeria Africa 2007 72.301 33333216 6223.367465 DZA 12
47 Angola Africa 2007 42.731 12420476 4797.231267 AGO 24
59 Argentina Americas 2007 75.320 40301927 12779.379640 ARG 32
In [36]:
fig = px.treemap(df, path=[px.Constant('World'),'continent','country'],
                values='pop', color='lifeExp', hover_data=['iso_alpha'])
fig.show()

icicle charts¶

In [37]:
df.head()
Out[37]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
11 Afghanistan Asia 2007 43.828 31889923 974.580338 AFG 4
23 Albania Europe 2007 76.423 3600523 5937.029526 ALB 8
35 Algeria Africa 2007 72.301 33333216 6223.367465 DZA 12
47 Angola Africa 2007 42.731 12420476 4797.231267 AGO 24
59 Argentina Americas 2007 75.320 40301927 12779.379640 ARG 32
In [38]:
fig = px.icicle(df, path=[px.Constant('World'), 'continent', 'country'],
                values='pop', color='lifeExp', hover_data=['iso_alpha'])
fig.show()

Distributions¶

histograms¶

In [39]:
df = px.data.tips()
df.head()
Out[39]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [40]:
fig = px.histogram(df, x='total_bill', y='tip', color='sex')
fig.show()
In [41]:
fig = px.histogram(df, x='total_bill', y='tip', color='sex', 
                   marginal='rug', hover_data=df.columns)
fig.show()

box plots¶

In [42]:
df.head()
Out[42]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [43]:
fig = px.box(df, x='day', y='total_bill', color='smoker', notched=True)
fig.show()

violin plots¶

In [44]:
df.head()
Out[44]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [45]:
fig = px.violin(df, y='tip', x='smoker', color='sex', 
                box=True, points='all', hover_data=df.columns)
fig.show()

Empirical cumulative distribution function (ECDF) charts¶

In [46]:
df.head()
Out[46]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [47]:
fig = px.ecdf(df, x='total_bill', color='sex')
fig.show()

strip charts¶

In [48]:
df.head()
Out[48]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
In [49]:
fig = px.strip(df, x='total_bill', y='time', orientation='h', color='smoker')
fig.show()

density contours or 2d histogram contours¶

In [50]:
df = px.data.iris()
df.head()
Out[50]:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1
In [51]:
fig = px.density_contour(df, x='sepal_width', y='sepal_length')
fig.show()

density heatmaps or 2d histograms¶

In [52]:
df.head()
Out[52]:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1
In [53]:
fig = px.density_heatmap(df, x='sepal_width', y='sepal_length', 
                        marginal_x='rug', marginal_y='histogram')
fig.show()

Images and Heatmaps¶

In [54]:
data1 = [[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
data1
Out[54]:
[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
In [55]:
fig = px.imshow(data1, 
            labels=dict(x='day of week',y='time of day',color='Productivity'),
                x=['Mon','Tue','Wed','Thu','Fri'], 
                y=['Morning','Afternoon','Evening'])
fig.update_xaxes(side='top')
fig.show()
In [56]:
from skimage import io

img = io.imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
fig = px.imshow(img)
fig.show()

The maps section is skipped.

Polar Coordinates¶

polar plots¶

In [57]:
df = px.data.wind()
df.head()
Out[57]:
direction strength frequency
0 N 0-1 0.5
1 NNE 0-1 0.6
2 NE 0-1 0.5
3 ENE 0-1 0.4
4 E 0-1 0.4
In [58]:
plasma_r_col = px.colors.sequential.Plasma_r
fig = px.scatter_polar(df, r='frequency', theta='direction', color='strength', 
                    symbol='strength', color_discrete_sequence=plasma_r_col)
fig.show()

radar charts¶

In [59]:
df.head()
Out[59]:
direction strength frequency
0 N 0-1 0.5
1 NNE 0-1 0.6
2 NE 0-1 0.5
3 ENE 0-1 0.4
4 E 0-1 0.4
In [60]:
fig = px.line_polar(df, r='frequency', theta='direction', color='strength',
                    line_close=True, color_discrete_sequence=plasma_r_col)
fig.show()
C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning:

The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

polar bar charts¶

In [61]:
df.head()
Out[61]:
direction strength frequency
0 N 0-1 0.5
1 NNE 0-1 0.6
2 NE 0-1 0.5
3 ENE 0-1 0.4
4 E 0-1 0.4
In [62]:
fig = px.bar_polar(df, r='frequency', theta='direction', color='strength',
                template='plotly_dark', color_discrete_sequence=plasma_r_col)
fig.show()

3D Coordinates¶

3D scatter plots¶

In [63]:
df = px.data.election()
df.head()
Out[63]:
district Coderre Bergeron Joly total winner result district_id
0 101-Bois-de-Liesse 2481 1829 3024 7334 Joly plurality 101
1 102-Cap-Saint-Jacques 2525 1163 2675 6363 Joly plurality 102
2 11-Sault-au-Récollet 3348 2770 2532 8650 Coderre plurality 11
3 111-Mile-End 1734 4782 2514 9030 Bergeron majority 111
4 112-DeLorimier 1770 5933 3044 10747 Bergeron majority 112
In [64]:
colorsmap = {'Joly':'blue', 'Bergeron':'green', 'Coderre':'red'}
fig = px.scatter_3d(df, x='Joly', y='Coderre', z='Bergeron', color='winner',
                size='total', hover_name='district', symbol='result',
                color_discrete_map=colorsmap)
fig.show()

Tenary Coordinates¶

In [65]:
df.head()
Out[65]:
district Coderre Bergeron Joly total winner result district_id
0 101-Bois-de-Liesse 2481 1829 3024 7334 Joly plurality 101
1 102-Cap-Saint-Jacques 2525 1163 2675 6363 Joly plurality 102
2 11-Sault-au-Récollet 3348 2770 2532 8650 Coderre plurality 11
3 111-Mile-End 1734 4782 2514 9030 Bergeron majority 111
4 112-DeLorimier 1770 5933 3044 10747 Bergeron majority 112
In [66]:
fig = px.scatter_ternary(df, a='Joly', b='Coderre', c='Bergeron', color='winner',
                    size='total', hover_name='district', size_max=12, color_discrete_map=colorsmap)
fig.show()

Dash¶

In [67]:
import plotly.graph_objects as go
fig = go.Figure()

# from dash import Dash, dcc, html
In [ ]: